class_name MainMenuScreen
extends Screen

const MENU_TEXTURES := [
	[preload("uid://cyivu2rlf8m2l"), preload("uid://dbdy765fqvs5y")],
	[preload("uid://cmx1k2rrhfst7"), preload("uid://ld4ilwd71x1h")]
]

@onready var selectable_menu_nodes: Array[TextureRect] = [%SinglePlayerTexture, %TwoPlayersTexture]
@onready var selection_icon: TextureRect = %SelectionIcon

var current_selected_index := 0
var is_active := false

func _ready() -> void:
	refresh_ui()
	
func _process(_delta: float) -> void:
	if is_active:
		if KeyUtils.is_action_just_pressed(Player.ControlScheme.P1, KeyUtils.Action.UP):
			change_selected_index(current_selected_index - 1)
		elif KeyUtils.is_action_just_pressed(Player.ControlScheme.P1, KeyUtils.Action.DOWN):
			change_selected_index(current_selected_index + 1)
		elif KeyUtils.is_action_just_pressed(Player.ControlScheme.P1, KeyUtils.Action.SHOOT):
			submit_selection()
			
func refresh_ui() -> void:
	for i in range(selectable_menu_nodes.size()):
		if i == current_selected_index:
			selectable_menu_nodes[i].texture = MENU_TEXTURES[i][1]
			selection_icon.position = selectable_menu_nodes[i].position + Vector2.LEFT * 25
		else:
			selectable_menu_nodes[i].texture = MENU_TEXTURES[i][0]
			
func change_selected_index(new_index: int) -> void:
	current_selected_index = clamp(new_index, 0, selectable_menu_nodes.size() -1 )
	SoundPlayer.play(SoundPlayer.Sound.UI_NAVIGATE)
	refresh_ui()
	
func submit_selection() -> void:
	SoundPlayer.play(SoundPlayer.Sound.UI_SELECT)
	var country_default := DataLoader.get_countries()[1]
	var player_two := "" if current_selected_index == 0 else country_default
	GameManager.player_setup = [country_default, player_two]
	screen_transition(SoccerGame.ScreenType.TEAM_SELECTION)
	
func set_active() -> void:
	refresh_ui()
	is_active = true
